﻿#include "deptvo.h"



DeptVO::DeptVO(QObject *parent): QBaseEntity(parent)
{
    clear();
}



DeptVO::~DeptVO()
{

}

QJsonObject DeptVO::toJsonObject(PropertiesType type,QStringList propertyList) const
{
    QJsonObject jsonObject;
    const QMetaObject *metaObject = this->metaObject();
    for (int i = 0; i < metaObject->propertyCount(); ++i) {
        QMetaProperty metaProperty = metaObject->property(i);
        if (metaProperty.isReadable()) {
            QVariant value = metaProperty.read(this);
            if (value.isValid()) {
                QString propertyName = metaProperty.name();
                // 忽略 objectName 属性
                if (propertyName == "objectName") {
                    continue;
                }
                if (metaProperty.userType() == qMetaTypeId<QList<DeptVO>>()) {
                    // 处理 QList<MyClass*> 类型的属性
                    QList<DeptVO> list = value.value<QList<DeptVO>>();
                    QJsonArray jsonArray;
                    for (DeptVO &item : list) {
                        jsonArray.append(item.toJsonObject());
                    }
                    jsonObject[propertyName] = jsonArray;
                }
                else {
                    jsonObject[propertyName] = qvariantToJsonValue(value);
                }
            }
        }
    }
    return jsonObject;
}

void DeptVO::fromJsonObject(const QJsonObject &jsonObject)
{
    const QMetaObject *metaObject = this->metaObject();
    for (int i = 0; i < metaObject->propertyCount(); ++i) {
        QMetaProperty metaProperty = metaObject->property(i);
        if (metaProperty.isWritable()) {
            QString propertyName = metaProperty.name();
            if (jsonObject.contains(propertyName)) {
                QJsonValue jsonValue = jsonObject[propertyName];
                if (jsonValue.isNull()) {
                    continue;
                }
                if (metaProperty.userType() == qMetaTypeId<QList<DeptVO>>()) {
                    QList<DeptVO> list;
                    QJsonArray jsonArray = jsonValue.toArray();
                    for (const QJsonValue &value : jsonArray) {
                        DeptVO child;
                        child.fromJsonObject(value.toObject());
                        list.append(child);
                    }
                    metaProperty.write(this, QVariant::fromValue(list));
                }else {
                    QVariant value = qjsonValueToVariant(jsonValue, metaProperty.type());
                    metaProperty.write(this, value);
                }
            }
        }
    }
}



void DeptVO::fromDept(const Dept &dept)
{
    QVariantMap deptProperties = dept.getAllProperties();
    QVariantMap deptVOProperties = getAllProperties();

    for (auto deptVOIt = deptVOProperties.begin(); deptVOIt != deptVOProperties.end(); ++deptVOIt) {
        QVariantMap::iterator deptIt = deptProperties.find(deptVOIt.key());
        if(deptIt != deptProperties.end()){
            this->setProperty(deptVOIt.key().toLatin1(),deptIt.value());
        }
    }
}

void DeptVO::toDept(Dept &dept)
{
    QVariantMap deptProperties = dept.getAllProperties();
    QVariantMap deptVOProperties = getAllProperties();

    for (auto deptIt = deptProperties.begin(); deptIt != deptProperties.end(); ++deptIt) {
        QVariantMap::iterator deptVOIt = deptVOProperties.find(deptIt.key());
        if(deptVOIt != deptVOProperties.end()){
            dept.setProperty(deptIt.key().toLatin1(),deptVOIt.value());
        }
    }
}

QList<DeptVO> DeptVO::getChildren() const
{
    return children;
}

void DeptVO::setChildren(const QList<DeptVO> &value)
{
    children = value;
}

uint64_t DeptVO::getId() const
{
    return id;
}

void DeptVO::setId(const uint64_t &value)
{
    id = value;
}

QString DeptVO::getName() const
{
    return name;
}

void DeptVO::setName(const QString &value)
{
    name = value;
}

uint64_t DeptVO::getPid() const
{
    return pid;
}

void DeptVO::setPid(const uint64_t &value)
{
    pid = value;
}

QString DeptVO::getRemark() const
{
    return remark;
}

void DeptVO::setRemark(const QString &value)
{
    remark = value;
}

int DeptVO::getStatus() const
{
    return status;
}

void DeptVO::setStatus(int value)
{
    status = value;
}
